import 'package:flutter/material.dart';
import 'package:flutter_color_plugin/flutter_color_plugin.dart';

class GesturePage extends StatefulWidget {
  const GesturePage({super.key});

  @override
  State<GesturePage> createState() => _GesturePageState();
}

class _GesturePageState extends State<GesturePage> {
  var printStr = "";
  double moveX = 0.0;
  double moveY = 0.0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        useMaterial3: true,
        colorScheme: ColorScheme.fromSeed(seedColor: ColorUtil.color('00ff00')),
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('手势'),
          leading: GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: const Icon(Icons.arrow_back)),
        ),
        body: FractionallySizedBox(
          widthFactor: 1,
          child: Stack(children: [
            Column(children: [
              GestureDetector(
                onTap: () => _printMsg('点击'),
                onDoubleTap: () => _printMsg('双击'),
                onLongPress: () => _printMsg('长按'),
                onTapCancel: () => _printMsg('取消'),
                onTapUp: (e) => _printMsg('松开'),
                onTapDown: (e) => _printMsg('按下'),
                child: Container(
                  padding: EdgeInsets.all(60),
                  decoration: BoxDecoration(color: ColorUtil.color('ff00ff')),
                  child: Text('点我'),
                ),
              ),
              Text(printStr)
            ]),
            Positioned(
              left: moveX,
              top: moveY,
              child: GestureDetector(
                onPanUpdate: (e) => _doMove(e),
                child: Container(
                  width: 60,
                  height: 60,
                  decoration: BoxDecoration(
                      color: Colors.amber,
                      borderRadius: BorderRadius.circular(30)),
                ),
              ),
            ),
          ]),
        ),
      ),
    );
  }

  void _printMsg(String msg) {
    setState(() {
      printStr += '_ ${msg}';
    });
  }

  void _doMove(DragUpdateDetails e) {
    setState(() {
      moveX += e.delta.dx;
      moveY += e.delta.dy;
    });
  }
}
